<?php

namespace IDock\Insurance\YAIC\Response;

use IDock\Http\Response;
use IDock\IDock\IDockEnums;
use IDock\Insurance\ACIC\ACICEnums;
use IDock\Insurance\YAIC\YAICEnums;
use IDock\Printer\Premium\PremiumBIPrinter;
use IDock\Printer\Premium\PremiumCIPrinter;
use IDock\Printer\Premium\PremiumCoveragePrinter;
use IDock\Printer\Premium\PremiumFactorPrinter;
use IDock\Printer\Premium\PremiumInquiryPrinter;
use IDock\Printer\Premium\PremiumTaxPrinter;

class PremiumInquiryResponse extends AllYAICResponse
{

    /**
     * @var PremiumInquiryPrinter $printer
     */
    public $printer = PremiumInquiryPrinter::class;

    protected $records = [];

    protected $result = [];

    public function printerInit(): Response
    {
        $body = $this->getBody();
        $data = $body['responseBody'] ?? [];
        if($data['reinsureFlag']){
            $this->printer->repeatMessage = '重复投保';
            $this->printer->success = false;
            return $this;
        }
        $ciMsg = $data['trafficInsurance']['bzBast']['reInsureItem'] ?? '';
        $biMsg = $data['commercial']['reinsure']['ReinsureDTO'][0] ?? [];
        if(!empty($ciMsg)){
            $this->getRepeatData($this->printer,$ciMsg,'');
            if ($this->printer->repeatInsuredFlg) {
                $this->printer->success = false;
                $this->printer->repeatMessage = $ciMsg;
                return $this;
            }
            $this->setError($ciMsg);
            return $this;
        }
        if(!empty($biMsg)){
            $this->printer->repeatInsurePrinter['biStartData'] = $biMsg['startDate'];
            $this->printer->repeatInsuredFlg = true;
            $this->printer->success = false;
        }

        $this->printer->ciPremiumPrinter = new PremiumCIPrinter();
        $this->printer->ciInquiryNo = $data['quotationNoCI'] ?? '';
        $this->printer->ciPremiumPrinter->ciStartData = $data['jqStartDate'] ?? '';
        $this->printer->ciPremiumPrinter->ciEndData = $data['jqEndDate'] ?? '';
        $this->printer->ciPremiumPrinter->ciPremium = $data['ciPremium'] ?? '';

        $this->printer->biPremiumPrinter = new PremiumBIPrinter();
        $this->printer->biInquiryNo = $data['quotationNoBI'] ?? '';
        $this->printer->biPremiumPrinter->biStartData = $data['syStartDate'] ?? '';
        $this->printer->biPremiumPrinter->biEndData = $data['syEndDate'] ?? '';
        $this->printer->biPremiumPrinter->biPremium = $data['biPremium'] ?? '';

        $this->printer->carTaxPrinter = new PremiumTaxPrinter();
        $this->printer->carTaxPrinter->carShipTax = $data['sumTax'] ?? 0;
        $this->printer->carTaxPrinter->thisYearTax = $data['carTax']['tax'] ?? 0;
        $this->printer->carTaxPrinter->overdue = $data['carTax']['taxOverdue'] ?? 0;

        //险种
        $coverageList = $this->formatArray($data['kinds']['KindDTO'] ?? []);
        foreach ($coverageList as $value) {
            $premiumCoveragePrinter = new PremiumCoveragePrinter();
            $premiumCoveragePrinter->premium = $value['premium'] ?? '';
            $premiumCoveragePrinter->amount = $value['amount'] ?? '';
            $coverageCode = $value['kindCode'];
            $premiumCoveragePrinter->coverageCode = array_search($coverageCode, YAICEnums::$coverageCode);
            $premiumCoveragePrinter->coverageName = IDockEnums::$coverageCode[$premiumCoveragePrinter->coverageCode];
            $this->printer->coverageListAppend($premiumCoveragePrinter);
        }
        $factorPrinter = new PremiumFactorPrinter();
        $factorPrinter->ComFactorAppend('车慧达货车分档','totalTranche',$data['chdSingleVehicleScore']['totalTranche'] ?? '');
        $factorPrinter->ComFactorAppend('车损分档预测','vehicleDamageGradePrediction',$data['chdSingleVehicleScore']['vehicleDamageGradePrediction'] ?? '');
        $factorPrinter->ComFactorAppend('交强险分档','vehicleDamageGradePrediction',$data['chdSingleVehicleScore']['vehicleDamageGradePrediction'] ?? '');
        $factorPrinter->ComFactorAppend('三者分档预测','theThreeAreForecast',$data['chdSingleVehicleScore']['theThreeAreForecast'] ?? '');
        $factorPrinter->ComFactorAppend('两客一危','guestStatus',$data['chdSingleVehicleScore']['guestStatus'] ?? '');
        $extInfo = $data['extendInfo']['ExtendInfoDTO'] ?? [];
        foreach ($extInfo as $value){
            if($value['key'] == 'dangerConditions'){
                $factorPrinter->ComFactorAppend('出险情况（商业险）','dangerConditions',$value['value'] ?? '');
            }
            if($value['key'] == 'peccancyadjustvalue'){
                $factorPrinter->ComFactorAppend('交通违法系数（商业险）','peccancyadjustvalue',$value['value'] ?? '');
            }
            if($value['key'] == 'claimAdjustValue'){
                $factorPrinter->ComFactorAppend('无赔优系数（商业险）','claimAdjustValue',$value['value'] ?? '');
            }
            if($value['key'] == 'claimAdjustLevel'){
                $factorPrinter->ComFactorAppend('无赔优系数级别（商业险）','claimAdjustLevel',$value['value'] ?? '');
            }
            if($value['key'] == 'claimTimes'){
                $factorPrinter->ComFactorAppend('连续承保期间出险次数（商业险）','claimTimes',$value['value'] ?? '');
            }
            if($value['key'] == 'relationType'){
                $factorPrinter->ComFactorAppend('关联单类型','relationType',$value['value'] ?? '');
            }
            if($value['key'] == 'deepPriceGradeCI'){
                $factorPrinter->ComFactorAppend('交强深度定价分档','deepPriceGradeCI',$value['value'] ?? '');
            }
            if($value['key'] == 'deepPriceGradeBI'){
                $factorPrinter->ComFactorAppend('商业深度定价分档','deepPriceGradeBI',$value['value'] ?? '');
            }
            if($value['key'] == 'pricingAdjustValue'){
                $factorPrinter->ComFactorAppend('自主定价系数','pricingAdjustValue',$value['value'] ?? '');
            }
            if($value['key'] == 'autoPriceHigh'){
                $factorPrinter->ComFactorAppend('自主定价（精算）高值','autoPriceHigh',$value['value'] ?? '');
            }
            if($value['key'] == 'autoPriceLow'){
                $factorPrinter->ComFactorAppend('自主定价（精算）低值','autoPriceLow',$value['value'] ?? '');
            }
            if($value['key'] == 'autoPriceBase'){
                $factorPrinter->ComFactorAppend('自主定价（精算）基准值','autoPriceBase',$value['value'] ?? '');
            }
            if($value['key'] == 'factormin'){
                $factorPrinter->ComFactorAppend('监管折扣下限','factormin',$value['value'] ?? '');
            }
            if($value['key'] == 'nocarScoreCI'){
                $factorPrinter->ComFactorAppend('车加非车评分交强','nocarScoreCI',$value['value'] ?? '');
            }
            if($value['key'] == 'nocarLevelCI'){
                $factorPrinter->ComFactorAppend('车加非车评级交强','nocarLevelCI',$value['value'] ?? '');
            }
            if($value['key'] == 'marginFlagCI'){
                $factorPrinter->ComFactorAppend('是否超过边际标志交强','marginFlagCI',$value['value'] ?? '');
            }
            if($value['key'] == 'nocarScoreBI'){
                $factorPrinter->ComFactorAppend('车加非车评分商业','nocarScoreBI',$value['value'] ?? '');
            }
            if($value['key'] == 'nocarLevelBI'){
                $factorPrinter->ComFactorAppend('车加非车评级商业','nocarLevelBI',$value['value'] ?? '');
            }
            if($value['key'] == 'priScore'){
                $factorPrinter->ComFactorAppend('车慧达家用车评分','priScore',$value['value'] ?? '');
            }
            if($value['key'] == 'noProfitReasonBI'){
                $factorPrinter->ComFactorAppend('不浮动原因代码（商业险）','noProfitReasonBI',$value['value'] ?? '');
            }
            if($value['key'] == 'claimAdjustValueCI'){
                $factorPrinter->ComFactorAppend('无赔优系数（交强险）','claimAdjustValueCI',$value['value'] ?? '');
            }
            if($value['key'] == 'noProfitReasonCI'){
                $factorPrinter->ComFactorAppend('不浮动原因代码（交强险）','noProfitReasonCI',$value['value'] ?? '');
            }
        }
        $this->printer->premiumFactorPrinter = $factorPrinter;

        return $this;
    }

    public function testHuashui()
    {

    }

    public function buildCoverage($isEnergy, array $orgList)
    {
        $orgList = array_combine(array_column($orgList, 'coverageCode'), $orgList);

        foreach ($this->printer->coverageList as &$coverage) {
            /*** @var $coverage PremiumCoveragePrinter */

            if ($isEnergy) {
                $coverage->coverageCode = 'C1' . substr($coverage->coverageCode, 2, 4);
                $coverage->coverageName = IDockEnums::$coverageCode[$coverage->coverageCode];
            }

            if (!$coverage->amount) {
                $coverage->amount = $orgList[$coverage->coverageCode]->amount ?? '';
            }
            $coverage->quantity = $orgList[$coverage->coverageCode]->quantity ?? '';
            $coverage->unitAmount = $orgList[$coverage->coverageCode]->unitAmount ?? '';
            //服务次数回写
            $coverage->serviceTimes = $orgList[$coverage->coverageCode]->serviceTimes ?? '';
        }

    }

    /**
     * 重复投保
     * @param PremiumInquiryPrinter $premiumInquiryPrinter
     * @param $ciMeg
     * @param $biMeg
     * @return void
     */
    public function getRepeatData(PremiumInquiryPrinter $premiumInquiryPrinter, $ciMeg, $biMeg)
    {
        if (preg_match('/终保日期 ([\d\s:-]+)/', $ciMeg, $matches)) {
            if ($matches[1] ?? '') {
                $ciStartData = date('Y-m-d H:i:s', strtotime($matches[1]));
                $premiumInquiryPrinter->repeatInsurePrinter['ciStartData'] = $ciStartData;
            }
        }

        if (preg_match('/终保日期：([\d]+)/', $biMeg, $matches)) {
            if ($matches[1] ?? '') {
                $biStartData = date('Y-m-d H:i:s', strtotime($matches[1]));

                $premiumInquiryPrinter->repeatInsurePrinter['biStartData'] = $biStartData;
            }
        }

        if ($premiumInquiryPrinter->repeatInsurePrinter['ciStartData'] or $premiumInquiryPrinter->repeatInsurePrinter['biStartData']) {
            $premiumInquiryPrinter->repeatInsuredFlg = true;
        }
    }
}
